<?php
session_start();
require_once 'include/db.php';
require_once 'include/userClass.php';
require_once 'include/postClass.php';
require_once 'include/functions.php';
require_once 'include/recaptchalib.php';
$privatekey = "6LeG6e8SAAAAAGNTpSdqrHPFj4mwCqC7NK9rY9Nv";

$post = new postClass ($user -> getUserID(), $db);

if(isset($_POST['cid']) && !isset($_POST['delete'])){ 		
						// When a comment is marked as inappropriate by anyone
	$action = $post -> flaggComment($_POST['cid']); 
}else if(isset($_POST['cid']) && (isset($_POST['delete'])) || isset($_POST['highligth'])){
	if(isset($_POST['highligth'])){ // Highlight comment that admin want to see
		break;
	}else{ 				//Delete comment, saves response message in $action
		$action = $post -> deleteComment($_POST['cid'], $user -> isUserAdmin());
	}
}else if (isset($_POST['pid'])){
	if(isset($_POST['delete'])) {
						//When post owner/admin deletes a post
		$action = $post -> deletePost($_POST['pid'], $user -> isUserAdmin());
	}
	else {				//When post is maked as inapropriate
		$action = $post -> flaggPost($_POST['pid']);
	}
}

						// When inserting a new comment to post
if(isset($_POST['author']) && isset($_POST['comment']) && isset($_GET['id'])){
	$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
	if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("reCAPTCHA ble ikke korrekt fylt inn. Venligst prøv igjen" .
         "(reCAPTCHA said: " . $resp->error . ")");
	} else {
		$comment = htmlspecialchars($_POST['comment']);
		$post -> insertComment($_POST['author'], $comment, $_GET['id'], $user ->userLoggedOn());
	}
}


$title = "Show post";
include 'include/header.php';
$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];
fbButtonJsScript();
?>
    <div id="content">
    <?php
	if(!isset($_GET['id']) || $_GET['id'] == "") {
		echo "</br><h4>Oooops, du har havnet på en side uten innhold...</h4></div>";
	}
	else {
	    if(isset($action)){
	    	echo "<br /><h4>{$action}</h4><br />";
	    }
    	echo $post -> displayPost($_GET['id'], $user -> isUserAdmin());?>
		<div class="fb-like" data-href="<?php echo $url; ?>" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>
    	<div id="commentField">
			<script type="text/javascript">
				var RecaptchaOptions = {
					custom_translations : {
                        instructions_visual : "Skriv ordene:",
                        instructions_audio : "Skriv hva du hører:",
                        play_again : "Spill av lyd på nytt",
                        cant_hear_this : "Last ned klippet i MP3",
                        visual_challenge : "Visuell test",
                        audio_challenge : "Hørselstest",
                        refresh_btn : "Få to nye ord",
                        help_btn : "Hjelp",
                        incorrect_try_again : "Ukorrekt, prøv igjen.",
                },
					theme : 'clean'
				};
			</script>
    		<?php // Get comments from DB and print comments out in a format based on the reader
    		$post -> getComments($_GET['id'], $user -> isUserAdmin(), ($user -> userLoggedOn())? $user -> userName:NULL, (isset($_POST['cid']) && isset($_POST['highligth']))? $_POST['cid'] : NULL);
    			// Allow users to comment post, if user are loggedin, username = commentauthor
    		$post -> newComment($_GET['id'], ($user -> userLoggedOn())? $user -> getUserID():'');?>    	
    	</div>
    </div>
    <div id="side">
    <?php
   	$user -> showUserInfo($user -> getUserInfo($_GET['id']));
	if($user -> userLoggedOn() && $user->userName == $post->getAuthor($_GET['id'])){
		if($user->isUserAdmin() == true) {
			echo "<a class='button' href='adminStat.php?usrName=".$user->userName."' title='Statistikk'>Brukeroversikt</a><br/>\n
			<a class='button' href='newAdmin.php?usrName=".$user->userName."' title='Ny admin'>Ny administrator</a><br/>
			<a class='button' href='displayUsr.php?usrName=".$user->userName."&own=true' title='Min blogg'>Min blogg</a><br/>";
		}
		echo "<a class='button' href='newPost.php?usrName=".$user->userName."' title='Nytt innlegg'><img class='icon' src='include/img/Edit.png' />Nytt innlegg</a><br/>\n
			<a class='button' href='editUser.php' title='Rediger profil'><img class='icon' src='include/img/Wheel.png' />Rediger profil</a>";
	}
	countVisits($db);
	}?>
    </div>

<?php include 'include/footer.php';?>
